/* USER CODE BEGIN Header */
/* you can remove the copyright */
/*
 *  Copyright 2020-2024 Yuntu Microelectronics co.,ltd
 *  All rights reserved.
 * 
 *  YUNTU Confidential. This software is owned or controlled by YUNTU and may only be
 *  used strictly in accordance with the applicable license terms. By expressly
 *  accepting such terms or by downloading, installing, activating and/or otherwise
 *  using the software, you are agreeing that you have read, and that you agree to
 *  comply with and are bound by, such license terms. If you do not agree to be
 *  bound by the applicable license terms, then you may not retain, install,
 *  activate or otherwise use the software. The production use license in
 *  Section 2.3 is expressly granted for this software.
 * 
 * @file main.c
 * @brief 
 * 
 */

/* USER CODE END Header */
#include "sdk_project_config.h"
/* Includes ------------------------------------------------------------------*/

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "version.h"

#include "ptmr.h"
#include "GPIO.h"
#include "flash.h"

#include "update.h"

#include "sys_config.h"
#include "aes.h"
#include "OTA_parse_msg.h"
#include "crc.h"
#include "update_app.h"

#include "adc.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function declare --------------------------------------------------*/
/* USER CODE BEGIN PFDC */
/* USER CODE END PFDC */
static void Board_Init(void);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void status_led_ctl(void)
{
	static uint32_t status_led = 0;
	if((system_tick_ms - status_led) > 500)
	{
		PINS_DRV_TogglePins(PORT_LED_GREEN, PIN_LED_GREEN);
		PINS_DRV_TogglePins(PORT_LED_RED, PIN_LED_RED);
		status_led = system_tick_ms;
//		uint32_t sec = system_tick_ms/1000;
//		debug_printf("BOOTLOADER run %d",sec);
	}
}

/* USER CODE END 0 */


/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */ 
    Board_Init();
    /* USER CODE BEGIN 2 */
	update_uart_init();
	pTMR_init();
	FLASH_init();
	ADC0_init();
	
	init_config_param();

	init_crc32_table();  // CRC32 ??????

	GPIO_SetBits(PORT_LED_GREEN, PIN_LED_GREEN|PIN_LED_RED);

	PRINTF("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
	PRINTF("\tBOOTLOADER\n");
	PRINTF("\tversion : %.3f\n",BOOT_VERSION);
	PRINTF("\tprotocol ver : %.3f\n",BOOT_PROTOCOL_VER);
	PRINTF("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
		status_led_ctl();
		OTA_parse_rx_message();
		ADC0_read();
//		OTA_Poll();
    }
    /* USER CODE END 3 */
}

static void Board_Init(void)
{
    CLOCK_SYS_Init(g_clockManConfigsArr,CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr,CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(CLOCK_MANAGER_ACTIVE_INDEX,CLOCK_MANAGER_POLICY_AGREEMENT);
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS0,g_pin_mux_InitConfigArr0);
    UTILITY_PRINT_Init();
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
